import { FMVCS } from "../../fgui/fmvc/FMVCS";
import GuideData from "./GuideData";
import { GlobalEvent } from "../global/Global";
import Manager from "../../manager/Manager";
import { ConstDefine } from "../common/ConstDefine";
import GuideView from "./GuideView";
import EventSystem from "../../core/EventSystem";

export default class GuideCtrl extends FMVCS.FCtrl {
    /** 数据实体 */
    static data: GuideData = null;

    constructor() {
        super();
        GuideCtrl.data = new GuideData();
    }

    /** 事件监听注册 */
    rigisterEventListener(){
        this.on(GlobalEvent.UIListenID.UI_GuideView_Open, this.onOpenView, this);
    }

    onOpenView(info: ConstDefine.Guide.EventItemData){
        let isGuide = GuideCtrl.data.setGuide(info);
        if(isGuide){
            info.obj.onClick(this.onGuideOneStepFinish, this);
            Manager.fui.open(ConstDefine.UI.GuideView, GuideView);
        }
    }

    closeView(){
        Manager.fui.close(ConstDefine.UI.GuideView);
    }

    onGuideOneStepFinish(event: fgui.Event){
        if(!GuideCtrl.data.getHaveNextStep()){
            this.closeView();
        }
        let target = event.currentTarget.$gobj as fgui.GButton;
        target.offClick(this.onGuideOneStepFinish, this);
        EventSystem.emit(GlobalEvent.UIListenID.UI_FinishOneGuideStep, GuideCtrl.data.curStep);
    }
}
